import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;


//VS4E -- DO NOT REMOVE THIS LINE!
public class AuctionDialog extends JDialog {

	Player orderedPlayer[] = new Player[8];
	Board board = new Board();
	
	int counter;
	int counter2;
	
	static int bid = 0;
	static int tradeeOffer = 0;
	
	URL myUrl;
	
	private JButton acceptButton;
	private JButton addBid;
	private JButton subtractBid;
	private JTextField bidderNameTextField;
	private JTextField bidderMoneyTextField;
	private JTextField bidTextField;
	private JLabel bidderToken;
	private JLabel background;
	
	private static final long serialVersionUID = 1L;
	
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public AuctionDialog() {
		initComponents();
	}
	
	private void initComponents() {
		setTitle("Monopoly");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/icon.png")));
		setResizable(false);
		setAlwaysOnTop(true);
		setLayout(new GroupLayout());
		
		add(acceptButton(), new Constraints(new Leading(210, 10, 10), new Leading(120, 10, 10)));
		add(addBid(), new Constraints(new Leading(240, 10, 10), new Leading(85, 10, 10)));
		add(subtractBid(), new Constraints(new Leading(120, 10, 10), new Leading(85, 10, 10)));
		add(bidderNameTextField(), new Constraints(new Leading(120, 10, 10), new Leading(20, 10, 10)));
		add(bidderMoneyTextField(), new Constraints(new Leading(120, 10, 10), new Leading(50, 10, 10)));
		add(bidTextField(), new Constraints(new Leading(136, 80, 10, 10), new Leading(80, 10, 10)));
		add(bidderToken(), new Constraints(new Leading(0, 10, 10), new Leading(10, 10, 10)));
		add(background(), new Constraints(new Leading(0, 10, 10), new Leading(0, 10, 10)));
		
		setSize(300, 160);
		setDefaultCloseOperation(AuctionDialog.DO_NOTHING_ON_CLOSE);
		getContentPane().setPreferredSize(getSize());
		pack();
		setLocationRelativeTo(null);
		setVisible(false);
	}
	
	private JTextField bidTextField() {
		if (bidTextField == null) {
			bidTextField = new JTextField();
			bidTextField.setEditable(true);
			bidTextField.setFont(new Font("Eras Light ITC", Font.PLAIN, 17));
			bidTextField.setOpaque(false);
			bidTextField.setAutoscrolls(false);
			bidTextField.setBorder(null);
			bidTextField.setEnabled(true);
			bidTextField.setText("0");
			bidTextField.setDisabledTextColor(Color.black);
		}
		return bidTextField;
	}
	
	private JTextField bidderMoneyTextField() {
		if (bidderMoneyTextField == null) {
			bidderMoneyTextField = new JTextField();
			bidderMoneyTextField.setEditable(false);
			bidderMoneyTextField.setFont(new Font("Eras Light ITC", Font.PLAIN, 14));
			bidderMoneyTextField.setOpaque(false);
			bidderMoneyTextField.setAutoscrolls(false);
			bidderMoneyTextField.setBorder(null);
			bidderMoneyTextField.setText("$ 15000000");
			bidderMoneyTextField.setEnabled(false);
			bidderMoneyTextField.setDisabledTextColor(Color.black);
		}
		return bidderMoneyTextField;
	}

	private JTextField bidderNameTextField() {
		if (bidderNameTextField == null) {
			bidderNameTextField = new JTextField();
			bidderNameTextField.setEditable(false);
			bidderNameTextField.setFont(new Font("Eras Light ITC", Font.PLAIN, 22));
			bidderNameTextField.setOpaque(false);
			bidderNameTextField.setAutoscrolls(false);
			bidderNameTextField.setBorder(null);
			bidderNameTextField.setEnabled(false);
			bidderNameTextField.setText("Anonymous");
			bidderNameTextField.setDisabledTextColor(Color.black);
		}
		return bidderNameTextField;
	}
	
	private JButton acceptButton() {
		if (acceptButton == null) {
			acceptButton = new JButton();
			acceptButton.setText("Accept");
			acceptButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					acceptButtonActionActionPerformed(event);
				}
			});
		}
		return acceptButton;
	}
	
	private JLabel background() {
		if (background == null) {
			background = new JLabel();
			background.setIcon(new ImageIcon(getClass().getResource("/images/auctionBG.jpg")));
		}
		return background;
	}
	
	public JButton subtractBid() {
		if (subtractBid == null) {
			subtractBid = new JButton();
			subtractBid.setIcon(new ImageIcon(getClass().getResource("/images/arrowLeft.png")));
			subtractBid.setBorderPainted(false);
			subtractBid.setOpaque(false);
			subtractBid.setBorder(null);
			subtractBid.setEnabled(false);
			subtractBid.setContentAreaFilled(false);
			subtractBid.setRolloverIcon(new ImageIcon(getClass().getResource("/images/arrowLeft_p.png")));
			subtractBid.setDisabledIcon(new ImageIcon(getClass().getResource("/images/arrowLeft.png")));
			subtractBid.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					subtractBidActionActionPerformed(event);
				}
			});
		}
		return subtractBid;
	}
	
	public JButton addBid() {
		if (addBid == null) {
			addBid = new JButton();
			addBid.setIcon(new ImageIcon(getClass().getResource("/images/arrowRight.png")));
			addBid.setBorderPainted(false);
			addBid.setOpaque(false);
			addBid.setBorder(null);
			addBid.setEnabled(false);
			addBid.setContentAreaFilled(false);
			addBid.setRolloverIcon(new ImageIcon(getClass().getResource("/images/arrowRight_p.png")));
			addBid.setDisabledIcon(new ImageIcon(getClass().getResource("/images/arrowRight.png")));
			addBid.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addBidActionActionPerformed(event);
				}
			});
		}
		return addBid;
	}
	private JLabel bidderToken() {
		if (bidderToken == null) {
			bidderToken = new JLabel();
			bidderToken.setIcon(new ImageIcon(getClass().getResource("/images/tokenDefault_b.png")));
		}
		return bidderToken;
	}
	
	public void loadVisuals(){
		bidderToken.setIcon(new ImageIcon(getClass().getResource("/images/token"+orderedPlayer[counter2].getAssignedLabel()+"_b.png")));
		bidderNameTextField.setText(orderedPlayer[counter2].getName());
		bidderMoneyTextField.setText("$ "+orderedPlayer[counter2].getMoney());
		switch(board.getBlock()[orderedPlayer[counter].getPosition()].getType()){
		case 1:
			bid=board.getBlock()[orderedPlayer[counter].getPosition()].getEstate().getPrice();
			break;
		case 2:
			bid=board.getBlock()[orderedPlayer[counter].getPosition()].getAirport().getPrice();
			break;
		case 3:
			bid=board.getBlock()[orderedPlayer[counter].getPosition()].getUtility().getPrice();
			break;
		}
		bidTextField.setText(""+bid);
		addBid.setEnabled(true);
		subtractBid.setEnabled(true);
	}
	
	@SuppressWarnings("unused")
	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}
	
	private void addBidActionActionPerformed(ActionEvent event) {
		bid=(Integer.parseInt(bidTextField.getText().trim()));
		if(orderedPlayer[counter2].checkMoney(bid)){
		bid+=100000;
		}
		bidTextField.setText(bid+"");
	}
	private void subtractBidActionActionPerformed(ActionEvent event) {
		bid=(Integer.parseInt(bidTextField.getText().trim()));
		if(bid>=110000){
			bid-=100000;
		}
		bidTextField.setText(bid+"");
	}
	private void acceptButtonActionActionPerformed(ActionEvent event) {
		if(Integer.parseInt(bidTextField.getText().trim())>=10000&&Integer.parseInt(bidTextField.getText().trim())<=orderedPlayer[counter2].getMoney()){
			bid=(Integer.parseInt(bidTextField.getText().trim()));
			if(orderedPlayer[counter2].checkMoneyOnAccept(bid)){
				orderedPlayer[counter2].setMoney(orderedPlayer[counter2].getMoney()-Integer.parseInt(bidTextField.getText().trim()));
				switch(board.getBlock()[orderedPlayer[counter].getPosition()].getType()){
					case 1:
						orderedPlayer[counter2].addOwnership(board.getBlock()[orderedPlayer[counter].getPosition()].getEstate(), orderedPlayer, counter2);
						break;
					case 2:
						orderedPlayer[counter2].addOwnership(board.getBlock()[orderedPlayer[counter].getPosition()].getAirport(), orderedPlayer, counter2);
						break;
					case 3:
						orderedPlayer[counter2].addOwnership(board.getBlock()[orderedPlayer[counter].getPosition()].getUtility(), orderedPlayer, counter2);
						break;
				}
				MainFrame.endAuction();
			}
		}
	}
	
}